<?php

class contMyCt extends contController
{
    public $action = 'index';
    
    private $aCss = array(
        'name' => 'cont.my',
        'file_list' => array()
    );
    
    private $aJs = array(
        'name' => 'cont.my2',
        'file_list' => array('util/spin', 'lib/jquery.form', 'cont/my')
    );
    
    public function index()
    {
        // 각 페이지에 필요한 CSS, JS 파일만 적용할 것.
        commViewer::_css($this->aCss);
        commViewer::_js($this->aJs);
        
        if (contCommon::isLogin() === false) {
            utilMust::redirect('/');
        }
        
        $aUserInfo = self::getUserInfo();
        
        // 레이아웃 표시 여부
        //$this->sTopLayoutView = false;
        //$this->sAsideLayoutView = false;
        //$this->sFooterLayoutView = false;
        
        // data assign
        $aAssignData = array(
            'name' => 'gudals',
            'aUserInfo' => $aUserInfo
        );
        
        // display view
        $this->view('/cont/my/index.html', $aAssignData);
    }
    
    /**
     * 회원 사진 수정
     */
    public function modify()
    {
        $this->params['user_num'] = commSession::session_get_data('sUserNum');
        
        $bParam = contCommon::chkRequestValiable($this->params, array('user_num' => 'int'));
        if ($bParam === false) {
            $aReturn = array('code' => 201, 'msg' => 'Parameter Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aParam = $this->params;
        
        // 파일 업로드
        $aParam['file_name'] = '';
        if (empty($_FILES['uploads']['name']) === false) {
            $utilImages = new utilImages();
            $utilImages->setThumbnailMake(450);
            $aReturnFile = $utilImages->upload($_FILES['uploads'], 'conts');
            if ($aReturnFile[0] != 200) {
                $aReturn = array('code' => $aReturnFile[0],'msg' => $aReturnFile[1]);
                jsonEncodePrint($aReturn);
            }
            $aParam['file_name'] = $aReturnFile[1];
        }
        
        $bResult = m('user_info')->modUserPhoto($aParam);
        if (!$bResult) {
            $aReturn = array('code' => 202, 'msg' => 'DB Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aReturn = array('code' => 200, 'res' => $aParam);
        jsonEncodePrint($aReturn);
    }
    
    /**
     * 회원 정보 수정
     */
    public function modUserInfo()
    {
        $this->params['user_num'] = commSession::session_get_data('sUserNum');
        
        $bParam = contCommon::chkRequestValiable($this->params, array('user_num' => 'int'));
        if ($bParam === false) {
            $aReturn = array('code' => 201, 'msg' => 'Parameter Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aParam = $this->params;
        
        $bResult = m('user_info')->modUserInfo($aParam);
        if (!$bResult) {
            $aReturn = array('code' => 202, 'msg' => 'DB Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aReturn = array('code' => 200);
        jsonEncodePrint($aReturn);
    }
    
    public static function getUserInfo()
    {
        $iUserNum = commSession::session_get_data('sUserNum');
        
        return m('user_info')->getInfo($iUserNum);
    }
}